package com.wushijia.highconcurrency.thread;

public class TestSync implements Runnable {

  private Timer timer = new Timer();
  private volatile int count = 0;

  public static void main(String[] args) {
    TestSync test = new TestSync();
    Thread t1 = new Thread(test);
    Thread t2 = new Thread(test);
    t1.setName("t1");
    t2.setName("t2");
    t1.start();
    t2.start();
  }

  @Override
  public void run() {
    for (int i = 0; i < 1000; i++) {
      count++;
      timer.add(Thread.currentThread().getName(), count);
    }
  }
}

class Timer {
  //private static int num = 0;
  public synchronized void add(String name, int i) {
    //synchronized (this) {
    //num++;
    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
    }
    System.out.println(name + ", 第" + i + "个timer");
    //}
  }
}
